package com.sys.server.modules.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.server.common.constants.enums.ResultSetEnum;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.CommentDto;
import com.sys.server.modules.controller.dto.IssueDto;
import com.sys.server.modules.controller.vo.CommentVo;
import com.sys.server.modules.controller.vo.FirstReplyVo;
import com.sys.server.modules.controller.vo.PageVo;
import com.sys.server.modules.controller.vo.SecondReplyVo;
import com.sys.server.modules.entity.FirstreplyEntity;
import com.sys.server.modules.entity.PostEntity;
import com.sys.server.modules.entity.SecondreplyEntity;
import com.sys.server.modules.mapper.FirstreplyMapper;
import com.sys.server.modules.mapper.PostMapper;
import com.sys.server.modules.mapper.SecondreplyMapper;
import com.sys.server.modules.service.IssueService;
import com.sys.server.modules.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {

    private static final Logger LOG = LoggerFactory.getLogger(IssueServiceImpl.class);

    @Resource
    private PostMapper postMapper;

    @Resource
    private FirstreplyMapper firstreplyMapper;

    @Resource
    private SecondreplyMapper secondreplyMapper;

    @Autowired
    private TokenService tokenService;

    @Override
    public R createIssue(IssueDto issueDto) {
        PostEntity entity = new PostEntity();
        entity.setReluser(tokenService.getCurrentUser());
        entity.setTitle(issueDto.getTitle());
        try {
            postMapper.insert(entity);
        } catch (Exception e) {
            LOG.error("发起讨论失败" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    @Override
    public R getIssueList(Long pagenum, Long pagesize) {
        IPage<PostEntity> page = new Page<>(pagenum, pagesize);
        try {
            postMapper.selectPage(page, null);
        } catch (Exception e) {
            LOG.error("分页获取讨论列表错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        PageVo result = new PageVo();
        result.setTotal(page.getTotal());
        result.setList(page.getRecords());
        result.setSize(pagesize);
        result.setCurrent(pagenum);
        return R.ok(ResultSetEnum.SUCCESS, result);
    }

    /**
     * 创建一级回复
     *
     * @param commentDto
     * @return
     */
    @Override
    public R createFirstComment(CommentDto commentDto) {
        FirstreplyEntity entity = new FirstreplyEntity();
        entity.setMaintext(commentDto.getMaintext());
        entity.setPostid(commentDto.getId());
        entity.setReltime(new Date());
        entity.setReluser(tokenService.getCurrentUser());
        try {
            firstreplyMapper.insert(entity);
        } catch (Exception e) {
            LOG.error("创建一级回复错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    /**
     * 创建二级回复
     *
     * @param commentDto
     * @return
     */
    @Override
    public R createSecondComment(CommentDto commentDto) {
        SecondreplyEntity entity = new SecondreplyEntity();
        entity.setMaintext(commentDto.getMaintext());
        entity.setReluser(tokenService.getCurrentUser());
        entity.setReltime(new Date());
        entity.setR1id(commentDto.getId());
        entity.setRepuser(commentDto.getRepuser());
        try {
            secondreplyMapper.insert(entity);
        } catch (Exception e) {
            LOG.error("创建二级回复错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    @Override
    public R getComment(Long postid) {
        CommentVo result = new CommentVo();
        try {
            PostEntity entity = postMapper.selectById(postid);
            result.setTitle(entity.getTitle());
        } catch (Exception e) {
            LOG.error("获取评论列表查询问题描述错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        /** 与这个问题相关的所有一级回复 */
        List<FirstreplyEntity> firstreplyEntityList =
                firstreplyMapper.selectList(Wrappers.<FirstreplyEntity>query().eq("postid", postid));
        List<FirstReplyVo> firstReplyVoList = new ArrayList<>();
        // 遍历一级回复
        for (FirstreplyEntity entity : firstreplyEntityList) {
            Long r1id = entity.getR1id();
            /** 这个一级回复的所有二级回复 */
            List<SecondreplyEntity> secondreplyEntityList =
                    secondreplyMapper.selectList(Wrappers.<SecondreplyEntity>query().eq("r1id", r1id));
            // 二级回复VO
            List<SecondReplyVo> secondReplyVoList = new ArrayList<>();
            // 遍历二级回复列表
            for (SecondreplyEntity secondreplyEntity : secondreplyEntityList) {
                SecondReplyVo vo = new SecondReplyVo();
                vo.setReltime(secondreplyEntity.getReltime());
                vo.setMaintext(secondreplyEntity.getMaintext());
                vo.setReluser(secondreplyEntity.getReluser());
                vo.setRepuser(secondreplyEntity.getRepuser());
                vo.setReplyShow(false);
                vo.setR2id(secondreplyEntity.getR2id());
                secondReplyVoList.add(vo);
            }
            FirstReplyVo firstReplyVo = new FirstReplyVo();
            firstReplyVo.setReply(secondReplyVoList);
            firstReplyVo.setReltime(entity.getReltime());
            firstReplyVo.setReluser(entity.getReluser());
            firstReplyVo.setMaintext(entity.getMaintext());
            firstReplyVo.setReplyShow(false);
            firstReplyVo.setR1id(r1id);
            firstReplyVoList.add(firstReplyVo);
        }
        result.setCommentList(firstReplyVoList);
        return R.ok(ResultSetEnum.SUCCESS, result);
    }
}
